//
//  UILabel_Ex.swift
//  Dawn
//
//  Created by cuicheng on 2021/9/11.
//

import UIKit
import YYText
public extension UILabel {

    func fitSize(maxSize: CGSize = CGSize(width: CGFloat.greatestFiniteMagnitude,
                                          height: CGFloat.greatestFiniteMagnitude)) {
        if let attr = attributedText, attr.string.count > 0 {
            size(attr.size(maxSize: maxSize))
        }else if let text = text, text.count > 0 {
            size(text.size(font: font, maxSize: maxSize))
        }
    }
    
    func fitHeight(maxSize: CGSize = CGSize(width: 60,
                                          height: CGFloat.greatestFiniteMagnitude),
                   padding: CGFloat = 0) {
        if let text = text, text.count > 0 {
           let _size = text.size(font: font, maxSize: maxSize)
            frame.size = CGSize(width: maxSize.width, height: _size.height + padding * 2)
        }else if let attr = attributedText, attr.string.count > 0 {
            let _size = attr.size(maxSize: maxSize)
            frame.size = CGSize(width: maxSize.width, height: _size.height + padding * 2)
        }
    }
    
    func fitWidth(maxSize: CGSize = CGSize(width: CGFloat.greatestFiniteMagnitude,
                                           height: 20),
                  padding: CGFloat = 0) {
        if let text = text, text.count > 0 {
           let _size = text.size(font: font, maxSize: maxSize)
            frame.size = CGSize(width: _size.width + padding * 2, height: maxSize.height)
        }else if let attr = attributedText, attr.string.count > 0 {
            let _size = attr.size(maxSize: maxSize)
            frame.size = CGSize(width: _size.width + padding * 2, height: maxSize.height)
        }
    }
    
    convenience init(font: UIFont, color: UIColor) {
        self.init()
        self.font = font
        self.textColor = color
    }
    
    func iconfont(_ text: String, fontSize: Double, fontName: String = "baseIcon") {
        self.text = text
        font = UIFont(name: fontName, size: fontSize.adapter)
    }
}

public extension YYLabel {

    func fitSize(maxSize: CGSize = CGSize(width: CGFloat.greatestFiniteMagnitude,
                                          height: CGFloat.greatestFiniteMagnitude)) {
        if let attr = attributedText, attr.string.count > 0 {
            size(attr.size(maxSize: maxSize))
        }else if let text = text, text.count > 0 {
            size(text.size(font: font, maxSize: maxSize))
        }
    }
    
    func fitHeight(maxSize: CGSize = CGSize(width: 60,
                                          height: CGFloat.greatestFiniteMagnitude),
                   padding: CGFloat = 0) {
        if let text = text, text.count > 0 {
           let _size = text.size(font: font, maxSize: maxSize)
            frame.size = CGSize(width: maxSize.width, height: _size.height + padding * 2)
        }else if let attr = attributedText, attr.string.count > 0 {
            let _size = attr.size(maxSize: maxSize)
            frame.size = CGSize(width: maxSize.width, height: _size.height + padding * 2)
        }
    }
    
    func fitWidth(maxSize: CGSize = CGSize(width: CGFloat.greatestFiniteMagnitude,
                                           height: 20),
                  padding: CGFloat = 0) {
        if let text = text, text.count > 0 {
           let _size = text.size(font: font, maxSize: maxSize)
            frame.size = CGSize(width: _size.width + padding * 2, height: maxSize.height)
        }else if let attr = attributedText, attr.string.count > 0 {
            let _size = attr.size(maxSize: maxSize)
            frame.size = CGSize(width: _size.width + padding * 2, height: maxSize.height)
        }
    }
}

